from MsgProcess import MsgProcess, MsgType
import time

class Dht_wifi(MsgProcess):
    '''万能开关插件'''
    def Text(self, message):
        Data = message['Data']

        #微信进入事件
        if type(Data) is dict  and 'initstate' in dict(Data).keys():
            if Data['initstate'] == 'onLoad':
                self.send(MsgType=MsgType.Text, Receiver="MqttProxy", Data={"type":"dhtWx"} )
            return 

        #判读是否是8266发过来的消息类型
        if isinstance(Data, dict) and 'type' in dict(Data).keys() and  Data['type'] == 'dhtWx' :
            Data=Data["message"]
            data = {'action':"dht","temperature": Data["temperature"]+"℃", "humidity": Data["humidity"]+"%","dhtTime":time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) }                 
            self.send(MsgType.Text, Receiver='MqttProxy', Data=data)
            return 

        #判读是否是8266发过来的消息类型
        if isinstance(Data, dict) and 'type' in dict(Data).keys() and  Data['type'] == 'dht' :
            Data=Data["message"]
            data = {'action':"dht","temperature": Data["temperature"]+"℃", "humidity": Data["humidity"]+"%","dhtTime":time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) }                 
            self.send(MsgType.Text, Receiver='MqttProxy', Data=data)
            self.say("温度{0}摄氏度 湿度{1}%".format(Data["temperature"],Data["humidity"]))
            return 

        else:
            self.send(MsgType=MsgType.Text, Receiver="MqttProxy", Data={"type":"dht"} )